# -*- coding: utf-8 -*-

import ctypes

class xyle_robot_control:
    # 设备信息申明
    DEV_IP = "61.177.75.106"
    DEV_LONG = len(DEV_IP)

    # 轨道机SDK文件路径
    DKYSDK_DLL_PATH = r'./TrackMachineControl.dll'
    SYSTEMSDK_DLL_PATH = r'./boost_system-vc141-mt-x64-1_67.dll'
    DATESDK_DLL_PATH = r'./boost_date_time-vc141-mt-x64-1_67.dll'

    def LoadSDK(self):
        '''
        加载SDK库
        '''
        ctypes.cdll.LoadLibrary(self.SYSTEMSDK_DLL_PATH)
        ctypes.cdll.LoadLibrary(self.DATESDK_DLL_PATH)
        Objdll = ctypes.cdll.LoadLibrary(self.DKYSDK_DLL_PATH)
        return Objdll

    def InitSDK(self,Objdll):
        '''
        初始化HCNetSDK库
        '''
        # 初始化DLL
        Objdll.Init()

    # 轨道机连接通知
    # 描述: 登陆成功或失败结果通知
    # data: 描述数据
    # size: 描述数据长度
    # userdata: 用户数据, Login时输入
    def ConnectCallback(self,data, size, userdata):
        return

    # 轨道机操作反馈
    # data: 返回数据
    # size: 返回数据长度
    # types:
    #   0 操作日志
    #   1 查询的X位置坐标, int容器取出
    #   2 查询的Y位置坐标, int容器取出
    # token: 登陆返回句柄
    # userdata: 用户数据, Login时输入
    def OperationCallback(self,types, data, size, token, userdata):
        return

    # SDK与轨道机网络断开通知
    # data: 描述数据
    # size: 描述数据长度
    # token: 登陆返回句柄
    # userdata: 用户数据, Login时输入
    def DisconnectCallback(self,dev_ads, size, token, userdata):
        return

    # 轨道机水平位置通知
    # xpos: 订阅的水平位置坐标
    # token: 登陆返回句柄
    # userdata: 用户数据, Login时输入
    def XposCallback(self,xpos, token, userdata):
        return

    # 轨道机垂直位置通知
    # ypos: 订阅的垂直位置坐标
    # token: 登陆返回句柄
    # userdata: 用户数据, Login时输入
    def YposCallback(self,ypos, token, userdata):
        return

    def Login(self,Objdll):
        '''
        登录设备
        '''
        # 回调函数
        connect_ck = ctypes.CFUNCTYPE(None, ctypes.POINTER(ctypes.c_char_p), ctypes.c_int, ctypes.POINTER(ctypes.c_void_p))
        operation_ck = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.POINTER(ctypes.c_char_p), ctypes.c_int, ctypes.c_ulong, ctypes.POINTER(ctypes.c_void_p))
        disconnect_ck = ctypes.CFUNCTYPE(None, ctypes.POINTER(ctypes.c_char_p), ctypes.c_int, ctypes.c_ulong, ctypes.POINTER(ctypes.c_void_p))
        xy_ck = ctypes.CFUNCTYPE(None, ctypes.c_int, ctypes.c_ulong, ctypes.POINTER(ctypes.c_void_p))
        userdata = ctypes.c_void_p()
        loginid = Objdll.Login(ctypes.c_char_p(self.DEV_IP.encode()), ctypes.c_int(self.DEV_LONG), connect_ck(self.ConnectCallback),
                               operation_ck(self.OperationCallback), disconnect_ck(self.DisconnectCallback), xy_ck(self.XposCallback),
                               xy_ck(self.YposCallback), userdata)
        return loginid